﻿package com.mui.controls
{
    import com.gengine.global.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import com.mui.skins.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.core.*;
    import flash.events.*;
    import flash.ui.*;

    public class GNumericStepper extends NumericStepper implements IToolTipItem, IFrUI
    {
        public const CLASSNAME:String = "List";
        private var _maxNumBtn:BaseButton;
        private var _minNumBtn:BaseButton;
        private var _bg:ScaleBitmap;
        public var style:String = "SetMaxNum";
        public var needDispatchEvent:Boolean;
        private var _isDisposed:Boolean;
        private var _styleName:String;
        private var _isStyleChange:Boolean = false;
        protected var _toolTipData:Object;
        private var _reactionTime:int = 500;
        public static const SetMaxNum:String = "SetMaxNum";
        public static const SetMaxAndMinNum:String = "SetMaxAndMinNum";
        public static const NoMaxAndMin:String = "NoMaxAndMin";
        static const MAX_BTN_STYLES:Object = {downSkin:"maxBtnDownSkin", overSkin:"maxBtnOverSkin", upSkin:"maxBtnDownSkin", disableSkin:"maxBtnDownSkin"};
        static const MIN_BTN_STYLES:Object = {downSkin:"minBtnDownSkin", overSkin:"minBtnOverSkin", upSkin:"minBtnDownSkin", disableSkin:"maxBtnDownSkin"};

        public function GNumericStepper()
        {
            this._styleName = this.CLASSNAME;
            this.imeMode = null;
            this.tabChildren = false;
            this.addEventListener(Event.ADDED_TO_STAGE, this.judgeToolTip);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.judgeToolTip);
            return;
        }// end function

        final override protected function configUI() : void
        {
            super.configUI();
            this.createChildren();
            this._maxNumBtn = new BaseButton();
            copyStylesToChild(this._maxNumBtn, MAX_BTN_STYLES);
            this._maxNumBtn.autoRepeat = true;
            this._maxNumBtn.setSize(20, 20);
            this._maxNumBtn.focusEnabled = false;
            addChild(this._maxNumBtn);
            this._maxNumBtn.addEventListener(MouseEvent.CLICK, this.setMaxNum);
            this._minNumBtn = new BaseButton();
            copyStylesToChild(this._minNumBtn, MIN_BTN_STYLES);
            this._minNumBtn.autoRepeat = true;
            this._minNumBtn.setSize(20, 20);
            this._minNumBtn.focusEnabled = false;
            addChild(this._minNumBtn);
            this._minNumBtn.addEventListener(MouseEvent.CLICK, this.setMinNum);
            this._bg = new ScaleBitmap();
            addChild(this._bg);
            return;
        }// end function

        private function setMaxNum(event:MouseEvent) : void
        {
            setValue(maximum);
            return;
        }// end function

        private function setMinNum(event:MouseEvent) : void
        {
            setValue(minimum);
            return;
        }// end function

        public function get styleName() : String
        {
            return this._styleName;
        }// end function

        public function set styleName(param1:String) : void
        {
            if (this._styleName != param1)
            {
                this._styleName = param1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }// end function

        final override protected function draw() : void
        {
            if (isInvalid(InvalidationType.STYLES))
            {
                if (this._isStyleChange)
                {
                    SkinManager.setComponentStyle(this, this._styleName);
                    this._isStyleChange = false;
                }
                this.updateStyle();
            }
            if (isInvalid(InvalidationType.DATA))
            {
                this.updateDate();
            }
            if (isInvalid(InvalidationType.SIZE))
            {
                this.updateSize();
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.SELECTED, InvalidationType.DATA))
            {
                this.updateDisplayList();
            }
            try
            {
                super.draw();
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        protected function createChildren() : void
        {
            return;
        }// end function

        protected function updateStyle() : void
        {
            return;
        }// end function

        protected function updateSize() : void
        {
            return;
        }// end function

        protected function updateDate() : void
        {
            return;
        }// end function

        protected function updateDisplayList() : void
        {
            return;
        }// end function

        override protected function setStyles() : void
        {
            copyStylesToChild(downArrow, DOWN_ARROW_STYLES);
            copyStylesToChild(upArrow, UP_ARROW_STYLES);
            copyStylesToChild(inputField, TEXT_INPUT_STYLES);
            copyStylesToChild(this._maxNumBtn, MAX_BTN_STYLES);
            copyStylesToChild(this._minNumBtn, MIN_BTN_STYLES);
            return;
        }// end function

        public function get toolTipData()
        {
            return this._toolTipData;
        }// end function

        public function set toolTipData(param1) : void
        {
            this._toolTipData = param1;
            this.judgeToolTip();
            return;
        }// end function

        public function get reactionTime() : int
        {
            return this._reactionTime;
        }// end function

        public function set reactionTime(param1:int) : void
        {
            this._reactionTime = param1;
            return;
        }// end function

        protected function judgeToolTip(event:Event = null) : void
        {
            if (event && event.type == Event.ADDED_TO_STAGE && this.toolTipData || !event && this.toolTipData && Global.stage.contains(this))
            {
                ToolTipsManager.register(this);
            }
            else
            {
                ToolTipsManager.unregister(this);
            }
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        override protected function onTextChange(event:Event) : void
        {
            if (this.needDispatchEvent)
            {
                setValue(Number(inputField.text));
            }
            else
            {
                super.onTextChange(event);
            }
            event.stopPropagation();
            this.checkValue();
            return;
        }// end function

        private function checkValue() : void
        {
            var _loc_1:* = Number(inputField.text);
            _loc_1 = _loc_1 > this.maximum ? (maximum) : (_loc_1);
            _loc_1 = _loc_1 < this.minimum ? (minimum) : (_loc_1);
            this.inputField.text = _loc_1.toString();
            return;
        }// end function

        override protected function drawLayout() : void
        {
            this.addChildAt(inputField, 0);
            var _loc_1:Number = 17;
            var _loc_2:Number = 11;
            inputField.setSize(width - _loc_1 - 1, height);
            upArrow.width = _loc_1;
            downArrow.width = _loc_1;
            upArrow.height = _loc_2;
            downArrow.height = _loc_2;
            if (this.style == NoMaxAndMin)
            {
                inputField.move(0, 0);
                var _loc_3:Boolean = false;
                this._maxNumBtn.visible = false;
                this._minNumBtn.visible = _loc_3;
            }
            else if (this.style == SetMaxNum)
            {
                inputField.move(0, 0);
                this._minNumBtn.visible = false;
            }
            else if (this.style == SetMaxAndMinNum)
            {
                inputField.move(this._minNumBtn.x + this._minNumBtn.width, 0);
                var _loc_3:Boolean = true;
                this._maxNumBtn.visible = true;
                this._minNumBtn.visible = _loc_3;
            }
            downArrow.move(width - _loc_1 - 1 + inputField.x, 9);
            upArrow.move(width - _loc_1 - 1 + inputField.x, 1);
            this._maxNumBtn.move(upArrow.x + upArrow.width, 0);
            downArrow.drawNow();
            upArrow.drawNow();
            inputField.drawNow();
            this._minNumBtn.drawNow();
            this._maxNumBtn.drawNow();
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            this._reactionTime = 500;
            ObjEventListerTool.delObjEvent(this);
            this.value = 1;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

        override protected function keyDownHandler(event:KeyboardEvent) : void
        {
            if (!enabled)
            {
                return;
            }
            event.stopImmediatePropagation();
            var _loc_2:* = Number(inputField.text);
            switch(event.keyCode)
            {
                case Keyboard.END:
                {
                    setValue(maximum);
                    break;
                }
                case Keyboard.HOME:
                {
                    setValue(minimum);
                    break;
                }
                case Keyboard.UP:
                {
                    setValue(nextValue);
                    break;
                }
                case Keyboard.DOWN:
                {
                    setValue(previousValue);
                    break;
                }
                case Keyboard.ENTER:
                {
                    setValue(_loc_2);
                    break;
                }
                case Keyboard.SPACE:
                {
                    event.stopImmediatePropagation();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
